//
//  TTMyOrderViewController.m
//  TaoTuo
//
//  Created by admin on 2019/9/29.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTMyOrderViewController.h"
#import <JXCategoryTitleView.h>
#import <JXCategoryIndicatorLineView.h>
#import <JXCategoryListContainerView.h>
#import "TTWaitConfirmViewController.h"
#import "TTMypageCountModel.h"

@interface TTMyOrderViewController ()<JXCategoryViewDelegate, JXCategoryListContainerViewDelegate>

@property (nonatomic, strong) JXCategoryTitleView *categoryView;
@property (nonatomic, strong) JXCategoryListContainerView *listContainerView;
@property (nonatomic, strong) NSArray *titles;
@property (nonatomic, strong) NSDictionary *statusDic;  // 订单状态

@end

@implementation TTMyOrderViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self getOrderCount];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.statusDic = @{
//        @"0" : @0,
        @"1" : @10,
        @"2" : @20,
        @"3" : @60,
        @"4" : @65,
        @"5" : @70,
    };
    
    self.title = @"订单管理";
    // 初始化JXCategoryTitleView
//    self.titles = @[@"待确认", @"处理中", @"待发货", @"待结算", @"全部"];
    self.titles = @[@"全部", @"待确认", @"处理中", @"待发货", @"已发货", @"待结算"];
    self.categoryView = [[JXCategoryTitleView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 54)];
    self.categoryView.delegate = self;
    self.categoryView.titleNumberOfLines = 2;
    self.categoryView.titleColor = UIColorHex(333333);
    self.categoryView.titleSelectedColor = UIColorHex(333333);
    self.categoryView.titles = self.titles;
    self.categoryView.titleColorGradientEnabled = YES;
    self.categoryView.titleFont = [UIFont systemFontOfSize:14];
    self.categoryView.titleSelectedFont = [UIFont systemFontOfSize:14];
    self.categoryView.contentEdgeInsetLeft = 15;        // 左边距
    self.categoryView.contentEdgeInsetRight = 15;       // 右边距
    [self.view addSubview:self.categoryView];
    
    // 添加指示器
    JXCategoryIndicatorLineView *lineView = [[JXCategoryIndicatorLineView alloc] init];
    lineView.indicatorColor = UIColorHex(9B71FF);
    lineView.indicatorHeight = 8;
    lineView.verticalMargin = 8;
    lineView.indicatorCornerRadius = 4;
    self.categoryView.indicators = @[lineView];
    
    
    // 初始化JXCategoryListContainerView并关联到categoryView
    self.listContainerView = [[JXCategoryListContainerView alloc] initWithDelegate:self];
    self.listContainerView.frame = CGRectMake(0, self.categoryView.bottom, SCREEN_WIDTH, SCREEN_HEIGHT - StatusBarAndNavBar_Height - self.categoryView.bottom - self.bottomSpace);
    [self.view addSubview:self.listContainerView];
    //关联到categoryView
    self.categoryView.contentScrollView = self.listContainerView.scrollView;
    
    self.categoryView.defaultSelectedIndex = self.defaultIndex;
    self.listContainerView.defaultSelectedIndex = self.defaultIndex;
    
    self.navigationController.interactivePopGestureRecognizer.enabled = (self.categoryView.selectedIndex == 0);
    
    /// 商品数量改变的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getOrderCount) name:@"goodsCountChange_notifyKey" object:nil];
    
    
}

- (void)backAction
{
    if (self.navigationController.viewControllers.count > 2) {
        [self.navigationController popToRootViewControllerAnimated:NO];
    } else {
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
}


#pragma mark - JXCategoryViewDelegate
- (void)categoryView:(JXCategoryBaseView *)categoryView didSelectedItemAtIndex:(NSInteger)index {
    //侧滑手势处理
    self.navigationController.interactivePopGestureRecognizer.enabled = (index == 0);

}

- (void)categoryView:(JXCategoryBaseView *)categoryView didScrollSelectedItemAtIndex:(NSInteger)index {
//    NSLog(@"%@", NSStringFromSelector(_cmd));
}

- (void)categoryView:(JXCategoryBaseView *)categoryView didClickSelectedItemAtIndex:(NSInteger)index {
    [self.listContainerView didClickSelectedItemAtIndex:index];
}

- (void)categoryView:(JXCategoryBaseView *)categoryView scrollingFromLeftIndex:(NSInteger)leftIndex toRightIndex:(NSInteger)rightIndex ratio:(CGFloat)ratio {
    [self.listContainerView scrollingFromLeftIndex:leftIndex toRightIndex:rightIndex ratio:ratio selectedIndex:categoryView.selectedIndex];
}



#pragma mark - JXCategoryListContainerViewDelegate
- (id<JXCategoryListContentViewDelegate>)listContainerView:(JXCategoryListContainerView *)listContainerView initListForIndex:(NSInteger)index {
    
    NSString *indexStr = [NSString stringWithFormat:@"%ld", index];
    int status = [self.statusDic[indexStr] intValue];
    TTWaitConfirmViewController *waitVC = [[TTWaitConfirmViewController alloc] initWithStatus:status];
    return waitVC;
}

- (NSInteger)numberOfListsInlistContainerView:(JXCategoryListContainerView *)listContainerView {
    return self.titles.count;
}


- (UIView *)listView
{
    return self.view;
}

/// 获取订单数量
- (void)getOrderCount
{
    @weakify(self)
    [[LLNetworkManagerTool sharedManager] requestMethod:GET urlString:myPageCount_api parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        @strongify(self)
        
        TTMypageCountModel *model = [TTMypageCountModel modelWithJSON:responseObject];
        
        NSArray *ddArray = [NSArray arrayWithObjects:model.AllCount, model.OdWaitConfirm, model.OdDoint, model.OdWaitSend, model.OdSend, model.OdWaitPay, nil];
        NSMutableArray *arr = @[].mutableCopy;
        for (int i = 0; i < ddArray.count; i++) {
            NSString *count = ddArray[i];
            [arr addObject:[self handleTitles:count title:self.titles[i]]];
        }
        self.categoryView.titles = arr;
        [self.categoryView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nullable error) {
        
        
    } showLoadingInView:nil];
}


- (NSString *)handleTitles:(NSString *)count title:(NSString *)title
{
    return [NSString stringWithFormat:@"%@\n%@", count, title];
}

@end
